<template>
  <van-icon
    @click="onCollect"
    :color="value ? '#ffa500' : ''"
    :name="value ? 'star' : 'star-o'"
  />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'CollectArticle',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async onCollect () {
      try {
        if (this.value) {
          await addCollect(this.articleId)
        } else {
          await deleteCollect(this.articleId)
        }
        this.$emit('input', !this.value) // 更新视图
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏') // 数据还需要取反，数据在这个函数体里还是上一个页面的数据
      } catch (error) {
        console.log(error)
        this.$toast.fail('操作失败，稍后重试')
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
